<?php

if (!class_exists("go_sql")){
include("/var/www/html/new/conf/le.je");
  $le=new go_sql();
  $le->connect();
}

class BD_zhida{
	private $appID = 'HkkAlxGkEvjoShBqR4NgCzjv';
	private $appsecret = 'kAQS3eYOhqwC7UhNLYDUpCDRd8Uz3y2V';
	private $reqURL_token = 'https://openapi.baidu.com/oauth/2.0/token/';// 请求URL
	private $token = '';
	private $provider_no = 'baidu';
	private $secret_key = '9b11ee316c818a2103c98c3507fe3146';
	
	//初始化数据
	private $ac='504025';  
	private $pw='5446f60483f3e3163189b84ff45054c6';

	public function __construct(){
		// $this->token = $this->getAccessToken();
		$this->inSide =new SoapClient(null,array(
			"location" => "http://112.124.14.164/open/openService/pft_insideMX.php",
			"uri" => "www.16u.com?ac_16u=16ucom|pw_16u=c33367701511b4f6020ec61ded352059|auth_16u=true")
		);
		$this->outSide = new SoapClient("http://open.12301.cc/openService/MXSE.wsdl",array('encoding' =>'UTF-8','cache_wsdl' => 0));
	}

	
	// 获取 ACCESS_TOKEN
	public function getAccessToken(){
		$token = array();
		if(file_exists('token.php')){ //已经生成
			$token = include_once 'token.php';		
		}else{ // 不存在 token.php 文件
			$token = $this->builAccessToken();
		}
	}
	//生成token
	public function builAccessToken(){
		$req = $this->reqURL.'?grant_type=client_credentials&client_id'.$this->appID.'&client_secret='.$this->appsecret;
		$token = file_get_contents($req);
	}
	
	
	//提交产品信息
	public function Post_bd_Product(){
		$Product_reqURL ='https://openapi.baidu.com/public/2.0/sku/sync';
		$sql="select tid_aid from uu_qunar_use where coop_id=1 and supplierIdentity='pro8888' ";
		$GLOBALS['le']->query($sql);
		$tid_aid_s = array();
		while($row = $GLOBALS['le']->fetch_assoc()){
			$tid_aid_s[]=$row;
		}
	
		foreach($tid_aid_s as $v){
			$tid_aid_array = explode("_",$v['tid_aid']);
			$tid = $tid_aid_array[0]; //门票id
			$aid = $tid_aid_array[1];  //供应商id
			
			$select_Ticket = "select a.id as lid,a.title as ltitle,a.jqts,b.title as ttitle ,a.address,a.delaytime,b.pay,b.buy_limit_up,b.buy_limit_low,b.delaydays,b.order_end,b.order_start,b.notes,b.tprice,b.ddays,b.pid,b.getaddr,b.cancel_auto_onMin,c.p_status,d.dhour  from uu_land a inner join uu_jq_ticket b on (a.id = b.landid) left join uu_products c on(b.pid = c.id) left join uu_land_f d on(b.id = d.tid) where b.id='$tid' and c.apply_limit = 1 and c.p_status = 0 limit 1";
			$GLOBALS['le']->query($select_Ticket);
			$str = $GLOBALS['le']->fetch_assoc();
			if($str['delaydays'] ==0 && $str['order_start']){
				$ticket_valid = $str['order_start'].' 至 '.$str['order_end'];
			}elseif($str['delaydays'] == 0){  
				$ticket_valid = '当天有效';
			}else{
				$ticket_valid = $str['delaydays'].'天内有效';
			}
			if($str['p_status']==0) $ticket_status=1; else $ticket_status=2;
			$ext =json_encode('预订须知:'.$str['jqts'].'   景区地址:'.$str['address'].'   门票有效期:'.$ticket_valid);
			
			$time = date('Y-m-d');
			//实际销售价格
			$price = $this->inSide->get_Dynamic_Price_Merge($str['pid'],$time,1,"","",1,"");
			if($price == -1) continue;
			//供应价格
			$supply_prices =$this->inSide->Dynamic_Price_And_Storage($this->ac,$str['pid'],$time,1,0,1,$aid); 
			if($supply_prices == 105 || $supply_prices ==1065 || $supply_prices ==103)
				continue;
			$supply_prices_xml =simpleXML_load_string($supply_prices); 
			$original_price = (string)$supply_prices_xml->Rec->UUsprice;
			$stock = (string)$supply_prices_xml->Rec->UUstorage;

			$tickets[] = array(
				't_id'=>$v['tid_aid'],  				//第三方（服务商）门票编号
				'sp_no'=>'001', 						//百度分配的店铺编号，支付时使用。
				'title'=>$str['ltitle'].$str['ttitle'], //门票名称
				'price'=>$price, 						//实际销售价格
				'original_price'=>$str['tprice']*100,   //原价
				'supply_price'=>$original_price, 		//供应价格
				'stock' =>$stock, 						//库存数量
				'status'=>$ticket_status, 				//票状态
				'ext'=>$ext, 							//其他数据，是一个k:v对象
			);
			$msg= json_encode($tickets);
		}
		$sign32 = $this->sign32($this->provider_no,$msg,$rtime,$this->secret_key);
		$rtime = time();
		$Product_data = array(
			'provider_no'=>$this->provider_no,
			'msg'=>$msg,
			'time'=>$rtime,
			'sign32'=>$sign32
		); 
		
		$Product_post = $this->bd_zhida_post($Product_data,$Product_reqURL);
		return $Product_post;
	}
	
	//加密
	public function sign32($provider_no,$msg,$rtime,$secret_key){
		return strtolower(md5(sprintf("%s%s%d%s",$provider_no,$msg,$rtime,$secret_key)));
	}
	
	
	//post提交
	public function bd_zhida_post($get_data,$RqUrl){  
	
		$str= http_build_query($get_data);
		$ch = curl_init();//打开
		curl_setopt($ch, CURLOPT_POST, true);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_URL, $RqUrl);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $str);
		$response  = curl_exec($ch);
		curl_close($ch);//关闭 
		$result = json_decode($response,true);
		return $result;
	}
	
	//记录文本
	public function logfile($txt,$path){
		$fp = fopen($path,"a");
		flock($fp, LOCK_EX) ;
		fwrite($fp,$txt."\n");
		flock($fp, LOCK_UN);
		fclose($fp); 
	}
}
$clinet = new BD_zhida();
$action = $_REQUEST['action'];
if($action == 'bd_product'){
	$txt = 
	$get_product = $clinet->Post_bd_Product();print_r($get_product);  //产品提交
}elseif($action == 'bd_submit_order'){
	echo 222;
}


?>